# This file loads the maps from input arguments


def read_tsp(tspFile):
    """Load map from a tsp file.
    """
    
    fd = None
    m = []
    import array
    def parse_line(i, line):

        return [float(nb) for nb in line.split()]
    try:
        fd = open(tspFile,'rb' )
        nb_cities = int(fd.readline())
        m = [0.0]*nb_cities
        for i, line in enumerate(fd.readlines()):
            m[i] = [0.0]*nb_cities
            for j, nb in enumerate(line.split()):
                m[i][j] = float(nb)
                m[j][i] = float(nb)
        
        #m = [parse_line(i, line) for i, line in enumerate( fd.readlines())]
        
        if len(m) != int(nb_cities):
            print("An error has occured, the file format is not correct")

        fd.close()
        return m
    except:
        print("A problem has occured using files, check the existance of the file")
        exit(1)
    finally:
        if fd is not None:
            fd.close()


def load_main():
    """ Load the first steps of search based on arguments.
    """
    import argparse,textwrap
    # Parse the arguments given to sokoban.py
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent('''\
        Assignment 4 : LINGI2261 - Artificial Intelligence
        ---------------------------------------------------
        Local search assignment
        '''), 
        epilog="---- Made by group 30 ----")
    parser.add_argument("tsp_file", action="store" , help="The TSP file")
    args = parser.parse_args( )
    tsp_file = args.tsp_file

    try:
        return read_tsp(tsp_file)  # percepts = tableau a 2 dimensions
    except (IOError) as e:
        print("Unable to load tsp file. Reason: %s", e)
        exit(1)
    